<?php
/*//防止双重转义  
if (get_magic_quotes_gpc()) {  
	$_POST = array_map('stripslashesDeep', $_POST);  
	$_GET = array_map('stripslashesDeep', $_GET);  
	$_COOKIE = array_map('stripslashesDeep', $_COOKIE);  
	$_SESSION = array_map('stripslashesDeep', $_SESSION);  
}*/

class CommonAction extends Action {
	protected $modelObject;
	protected $pageInfo = array();
	protected $adminInfo;
	
    public function __construct(){
		parent::__construct();
		header('Content-Type:text/html; charset='.C('DEFAULT_CHARSET'));
		
		/*
		MODULE_NAME 当前模块名  
		ACTION_NAME 当前操作名  
		*/
		if(!($this->adminInfo = session('adminInfo'))){
			redirect(U('/Login'));
		}
		
		if($this->adminInfo['role_id'] > 0){//如果 role_id 为0 表示是超级管理员。无需判断权限
			//第一步：获取用户的access list
			if(!($roleInfo = session('sessionRoleInfo'))){
				$roleInfo = D('Role')->getInfo($this->adminInfo['role_id']);
				session('sessionRoleInfo', $roleInfo);
			}
			if(!($allAccessList = session('sessionAccessList'))){
				$allAccessList = D('Access')->getAll();
				session('sessionAccessList', $allAccessList);
			}
			$accessList = array();
			$roleAccess = explode(',', $roleInfo['access_list']);
			foreach($allAccessList as $key=>$val){
				if(in_array($val['id'], $roleAccess)){
					$accessList[] = $val;
				}
			}
			
			$allAccessListArray = $accessListArray = array();
			foreach($accessList as $key=>$val){
				$accessListArray[] = strtolower($val['module'].'-'.$val['action']);
			}
			
			foreach($allAccessList as $key=>$val){
				$allAccessListArray[] = strtolower($val['module'].'-'.$val['action']);
			}
			//第二步：判断:当前MODULE_NAME ACTION_NAME是否有权限节, 如果没有在权限树里面的权限直接就有权限
			$nowModuleAndAction = strtolower(MODULE_NAME.'-'.ACTION_NAME);
			if(strtolower(ACTION_NAME) == 'doaction' && isset($_POST['act'])){
				$nowModuleAndAction = strtolower(MODULE_NAME.'-'.$_POST['act']);	
			}
			
			if(in_array($nowModuleAndAction, $allAccessListArray) && !in_array($nowModuleAndAction, array_merge($accessListArray,C('ACCESS_LIST')))){
				$this->error('无权操作');	
			}
		}
    }
	
	public function add(){
		if(IS_POST){
			$_POST['add_time'] = NOW_TIME;
			if($this->modelObject->create()){
				if($this->modelObject->add()){
					$this->success('插入数据成功', U('index'));	
				}else{
					$this->error('插入失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$this->display();	
		}
	}
	
	public function edit(){
		if(IS_POST){
			$_POST['update_time'] = NOW_TIME;
			if($this->modelObject->create()){
				if($this->modelObject->save()){
					$this->success('编辑成功', U('index', session('pageInfo')));	
				}else{
					$this->error('编辑失败!'.$this->modelObject->getError());
				}
			}else{
				$this->error('创建数据失败!'.$this->modelObject->getError());
			}
		}else{
			$id = I('id', 0, 'intval');
			if($id){
				$info = $this->modelObject->getInfo($id);
				
				$this->assign('info', $info);
				$this->display();	
			}else{
				$this->error('参数错误!');
			}
		}
	}

	public function doAction(){
		$action = I('act', '');
		if(empty($action)){
			$this->error('请选择你要的操作!');	
		}else{
			$this->$action();	
		}
	}
	
	// 删除页面缓存
    public function delCache(){
		import("ORG.Io.Dir");
		$dir = new Dir();
		@$dir->del(RUNTIME_PATH.'Cache/'); //删除page cache
		@$dir->del(RUNTIME_PATH.'Logs/'); //删除file cache
		@$dir->del(RUNTIME_PATH.'Data/'); //删除file cache
		@$dir->del(RUNTIME_PATH); //删除runtime文件
		
		$this->success('清除成功!', U('/index/main'));
    }
	
	public function uploadEditor(){
		uploadEditor('imgFile', C('TMPL_PARSE_STRING.__UPLOADEDITOR__'));
	}
}//end class